﻿using Serilog;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Helpers
{
    public class SerilogHelperOptions
    {
        /// <summary>
        /// exceptionless用户名称
        /// </summary>
        public string UserIdentity { get; set; }
        /// <summary>
        /// 文件输出的默认模板
        /// </summary>
        public string FileOutputTemplate { get; set; } = "{Timestamp:HH:mm:ss} {Level:u3} {userName}{name}{user} 【{Message:lj}】 {SourceContext:l}{NewLine}{Exception}";
        /// <summary>
        /// 控制台输出的默认模板
        /// </summary>
        public string ConsoleOutputTemplate { get; set; } = "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}";
        /// <summary>
        /// 日志路径
        /// </summary>
        public string LogPath { get; set; }
        /// <summary>
        /// 日志的最低输出等级
        /// </summary>
        public LogEventLevel MinimumLevel { get; set; } = LogEventLevel.Information;
        /// <summary>
        /// 是否记录到控制台
        /// </summary>
        public bool IsLogToConsole { get; set; } = false;
        /// <summary>
        /// 是否捕获全局异常
        /// </summary>
        public bool IsLogUnhandledException { get; set; } = true;
        public UnhandledExceptionEventHandler UnhandledException { get; set; } = (s, e) =>
        {
            try
            {
                Exception ex = e.ExceptionObject as Exception;
                Log.Fatal(ex, "UnhandledException");
            }
            catch (Exception)
            {
            }
        };

        public EventHandler<UnobservedTaskExceptionEventArgs> UnobservedTaskException { get; set; } = (s, e) =>
        {
            try
            {
                Log.Fatal(e.Exception.GetBaseException(), "UnobservedTaskException");
                e.SetObserved();
            }
            catch (Exception)
            {
            }
        };
    }
}
